import 'package:amap_flutter/amap_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';

import 'home.dart';
import 'l10n/app_localizations.dart';

void main() {
  final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();

  // amap init
  AMapFlutter.init(
    apiKey: ApiKey(
      iosKey: "a4a1394fe817c2f86a424b897b4a9af4",
      androidKey: "d0065c21d2aedd0b234bfb7b88e5d6b2",
      webKey: "fc9908dc4103f3d8274070bb34ab37af",
    ),
    agreePrivacy: true,
  );

  /// Preserve the native splash screen while the app is initializing.
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  /// 不同类型的中文
  static const List<Locale> zhLocales = [
    Locale.fromSubtags(languageCode: 'zh'), // generic Chinese 'zh'
    Locale.fromSubtags(
      languageCode: 'zh',
      scriptCode: 'Hans',
    ), // generic simplified Chinese 'zh_Hans'
    Locale.fromSubtags(
      languageCode: 'zh',
      scriptCode: 'Hant',
    ), // generic traditional Chinese 'zh_Hant'
    Locale.fromSubtags(
      languageCode: 'zh',
      scriptCode: 'Hans',
      countryCode: 'CN',
    ), // 'zh_Hans_CN'
    Locale.fromSubtags(
      languageCode: 'zh',
      scriptCode: 'Hant',
      countryCode: 'TW',
    ), // 'zh_Hant_TW'
    Locale.fromSubtags(
      languageCode: 'zh',
      scriptCode: 'Hant',
      countryCode: 'HK',
    ), // 'zh_Hant_HK'
  ];

  @override
  Widget build(BuildContext context) {
    /// remove the native splash screen after the app is initialized
    Future.delayed(const Duration(seconds: 2), () {
      FlutterNativeSplash.remove();
    });

    return MaterialApp(
      home: const HomePage(),

      /// manually set the locale
      // // internationalization support
      // localizationsDelegates: [
      //   GlobalMaterialLocalizations.delegate,
      //   GlobalCupertinoLocalizations.delegate,
      //   GlobalWidgetsLocalizations.delegate,
      //   // generated localizations delegate by intl
      //   AppLocalizations.delegate,
      // ],
      // supportedLocales: [
      //   Locale('en'),
      //   Locale('zh'),
      // ],

      /// automatically set the locale based on device settings
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: [
        ...AppLocalizations.supportedLocales,
        // ...zhLocales, // add different types of Chinese locales
      ],

      /// the localized title of the app
      onGenerateTitle: (context) => AppLocalizations.of(context)!.helloWorld,

      /// resolve selected locale
      localeResolutionCallback: (locale, supportedLocales) {
        // If the device locale is not supported, return the first supported locale
        if (locale == null || !supportedLocales.contains(locale)) {
          return supportedLocales.first;
        }
        return locale;
      },
    );
  }
}
